package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwYsclgjDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwLyqsclqksm;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwYsclgj;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwYsclgjService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.YwHwYsclgjForm;

/*
 * 陆域生活垃圾清扫车辆情况service实现类
 */
@Service
@Transactional
public class YwHwYsclgjServiceImpl  implements IYwHwYsclgjService{
	@Autowired
	private YwHwYsclgjDao ywHwYsclgjDao;
	
	/**
	 * 获取form
	 */
	public YwHwYsclgjForm getForm(Long Id) {
		YwHwYsclgj entity=ywHwYsclgjDao.get(Id);
		YwHwYsclgjForm form=new DozerBeanMapper().map(entity,YwHwYsclgjForm.class);
		return form;
	}

	public List<YwHwYsclgjForm> getListByQyId(Long qyId) {
		List<YwHwYsclgj> list=this.getListByQyid(qyId);
		List<YwHwYsclgjForm> forms=new ArrayList<YwHwYsclgjForm>();
		for(YwHwYsclgj entity:list){
			YwHwYsclgjForm form=new DozerBeanMapper().map(entity,YwHwYsclgjForm.class);
			forms.add(form);
		}
		return forms;
	}

	/**
	 * 根据ywid查询数据
	 * @param QYId
	 * @return
	 */
	private List<YwHwYsclgj> getListByQyid(Long qyId){
		String sql=" from "+YwHwYsclgj.class.getName()+" where  qyid="+qyId;
		List<YwHwYsclgj> list=this.ywHwYsclgjDao.find(sql);
		return list;
	}


	public void SaveOrUpdate(YwHwYsclgjForm form) {
		YwHwYsclgj entity=new DozerBeanMapper().map(form,YwHwYsclgj.class);
		this.ywHwYsclgjDao.save(entity);
	}

	public void SaveOrUpdate(YwHwYsclgj entity) {
		this.ywHwYsclgjDao.save(entity);
	}

	public void parseYwHwYsclgj(HSSFSheet hssfsheet,Long qyid,Long ywid, String templateCode){
		String hql = "from YwHwYsclgj t where t.ywid = '"+ywid+"' and t.tplcode= '"+templateCode+"'";
		List<YwHwYsclgj> list = new ArrayList<YwHwYsclgj>();
		list = this.ywHwYsclgjDao.find(hql, list);
		for(int i=0; i<list.size();i++){
			this.ywHwYsclgjDao.delete(list.get(i));
		}
		try {
			
			HSSFRow hssfrow = hssfsheet.getRow(0);// 第一行
			for (int i = 2; i < hssfsheet.getPhysicalNumberOfRows()-2; i++) {
				String lx;
				String sl;
				String xh;
				hssfrow = hssfsheet.getRow(i);
				// 判断是否还存在需要导入的数据
				if (hssfrow != null) {
					if (hssfrow.getCell((short) 1) == null) {
						lx = "";
					} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
						lx = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						lx = hssfrow.getCell((short) 1).getStringCellValue().trim();
					}
					if (hssfrow.getCell((short) 2) == null) {
						sl = "";
					} else if (hssfrow.getCell((short) 2).getCellType() == 0) {
						sl = new Double(hssfrow.getCell((short) 2).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						sl = hssfrow.getCell((short) 2).getStringCellValue().trim();
					}
					if (hssfrow.getCell((short) 3) == null) {
						xh = "";
					} else if (hssfrow.getCell((short) 3).getCellType() == 0) {
						xh = new Double(hssfrow.getCell((short) 3).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						xh = hssfrow.getCell((short) 3).getStringCellValue().trim();
					}
					if(StringUtils.isEmpty(lx)){
					}else{
						YwHwYsclgj ywHwYsclgj = new YwHwYsclgj();
						ywHwYsclgj.setLx(lx);
						ywHwYsclgj.setQyid(qyid);
						ywHwYsclgj.setSl(sl);
						ywHwYsclgj.setTplcode(templateCode);
						ywHwYsclgj.setXh(xh);
						ywHwYsclgj.setYwid(ywid);
						this.SaveOrUpdate(ywHwYsclgj);
					}
				}
					
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * /获取车辆经营的号码
	 * @param id
	 * @param tempcode 业务编码
	 * @return
	 */
	public List<YwHwYsclgj> getCheckCar(Long id, String tempcode) {
		String hql=" from "+YwHwYsclgj.class.getName()+" where ywid="+id+
					" and tplcode='"+tempcode+"'";
		return this.ywHwYsclgjDao.find(hql);
	}
	
}
